# define _CRT_SECURE_NO_WARNINGS
#include"seqlist.h"

void Init_Seqlist(SL* ps)
{
	assert(ps);
	SL* pc = (SLDatatype*)calloc(DEFAULT_CAP, sizeof(SLDatatype));
	if (pc == NULL)
	{
		perror("Init_Seqlist");
		return;
	}
	ps->a = pc;
	ps->sz = 0;
	ps->capacity = DEFAULT_CAP;
}

void SLDestory(SL* ps)
{
	assert(ps->a);
	free(ps->a);
	ps->a = NULL;
	ps->sz = 0;
	ps->capacity = 0;
}

void SLPrint(SL* ps)
{
	assert(ps);
	int i = 0;
	for (i = 0; i < ps->sz; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}



void Is_AddCap(SL* ps)
{
	assert(ps);
	if (ps->sz == ps->capacity)
	{
		SLDatatype* ptr = (SLDatatype*)realloc(ps->a, sizeof(SLDatatype)* 2 * ps->capacity);
		if (ptr == NULL)
		{
			perror("Is_AddCap");
			return;
		}
		ps->capacity *= 2;
		ps->a = ptr;
	}

}

void SLPushBack(SL* ps, int x)
{
	assert(ps);
	Is_AddCap(ps);
	ps->a[ps->sz++] = x;
}


void SLPopBack(SL* ps)
{
	assert(ps);
	//ps->a[ps->sz - 1] = 0;
	ps->sz--;
}


void SLPushFront(SL* ps, int x)
{
	assert(ps);
	Is_AddCap(ps);
	for (int end = ps->sz - 1;end>=0;end--)
	{
		ps->a[end + 1] = ps->a[end];
	}
	ps->a[0] = x;
	ps->sz++;
}


void SLPopFront(SL* ps)
{
	assert(ps);
	int i = 0;
	for (i = 0; i < ps->sz-1; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->sz--;
}
